<!DOCTYPE HTML>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div>
    当前用户：<span id="currentUser"></span><br/>
    发给：<input type="text" id="toUser"><br/>
    消息：<input type="text" id="msgInput" style="width: 400px;"><br/>
    <input type="submit" value="Start" onclick="start()"/>
</div>
<div id="messages"></div>
<script type="text/javascript">
    var userName = "090707f5-2c44-415e-a385-15f706081d37";
    if (!userName || userName == '') {
        userName = randomString(4)
        sessionStorage.setItem("name", userName);
    }
    $('#currentUser').html(userName);
    var webSocket = new WebSocket('ws://localhost:8813/websocket/' + userName);

    webSocket.onerror = function (event) {
        onError(event)
    };

    webSocket.onopen = function (event) {
        onOpen(event)
    };

    webSocket.onmessage = function (event) {
        onMessage(event)
    };

    function onMessage(event) {
        var msg = JSON.parse(event.data)
        console.log(msg)
        if (msg && msg.ping && msg.ping != '') {
            webSocket.send('{"pong":"' + msg.ping +
                            '","identity":"' + "driver" +
                            '","token":"' + userName +
                            '","userLongitude":"' + "111.1111" +
                            '","userLatitude":"' + "112.1111" + '"}');
        } else {
            document.getElementById('messages').innerHTML += '<br />' + msg.msg.startTime;
        }
    }

    function onOpen(event) {
        document.getElementById('messages').innerHTML
            = 'Connection established';
    }

    function onError(event) {
        alert(event.data);
    }

    function start() {
        let toUser = "efdfd9c5-ab7e-4e93-8e54-ad6742671461"
        let orderNumber = "4195873341519328"
        webSocket.send('{"to":"' + toUser + '","identity":"' + "driver" +
            '","msg": "' + orderNumber +
            '","userLongitude":"' + "113.406185" +
            '","userLatitude":"' + "22.531913" + '"}');
    }


    function randomString(len) {
        len = len || 32;
        var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        var maxPos = $chars.length;
        var pwd = '';
        for (i = 0; i < len; i++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    }
</script>
</body>
</html>